﻿//提示信息框
function ERP_alert(msg) {
    $.messager.alert('提示', msg);
}
//错误提示信息框 type可用的值是：error、question、info、warning。
function ERP_alertError(msg) {
    $.messager.alert('提示', msg, 'error');
}
//错误提示信息框 type可用的值是：error、question、info、warning。
function ERP_alertQuestion(msg) {
    $.messager.alert('提示', msg, 'question');
}
//错误提示信息框 type可用的值是：error、question、info、warning。
function ERP_alertInfo(msg) {
    $.messager.alert('提示', msg, 'info');
}
//错误提示信息框 type可用的值是：error、question、info、warning。
function ERP_alertWarning(msg) {
    $.messager.alert('提示', msg, 'warning');
}
//提示信息框
function ERP_confirm(msg,fun) {
     $.messager.confirm('提示', msg,fun);
}
//easyuitree  初始化方法
function convert(rows) {
    function exists(rows, parentId) {
        for (var i = 0; i < rows.length; i++) {
            if (rows[i].id == parentId) return true;
        }
        return false;
    }
    var nodes = [];
    // get the top level nodes
    for (var i = 0; i < rows.length; i++) {
        var row = rows[i];
        if (!exists(rows, row.parentId)) {
            nodes.push({
                id: row.id,
                text: row.name,
                checked: row.checked
            });
        }
    }

    var toDo = [];
    for (var i = 0; i < nodes.length; i++) {
        toDo.push(nodes[i]);
    }
    while (toDo.length) {
        var node = toDo.shift();	// the parent node
        // get the children nodes
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            if (row.parentId == node.id) {
                var child = { id: row.id, text: row.name, checked: row.checked };
                if (node.children) {
                    node.children.push(child);
                } else {
                    node.children = [child];
                }
                toDo.push(child);
            }
        }
    }
    return nodes;
}

//物料列表仓库列下拉选项绑定
function datagridWareBand(e, rowIndex, wareID, productID, packID, sku) {
    var code = '';
    var codeType = 's';
    if (productID != null && productID != '') {
        code = productID;
        codeType = 't';
    }
    else if (packID != null && packID != '') {
        code = packID;
        codeType = 'b';
    }
    else {
        code = sku;
        codeType = 's';
    }
    var editors = e.datagrid("getEditors", rowIndex);
    var wareEditor = editors[0].target.combobox({
        url: "/Storage/Warehouse/GetMaterialCanStockOutWarehouseList?code=" + code + "&type=" + codeType,
        valueField: 'Text',
        textField: 'Text',
        required: true
    });
    editors[0].target.combobox("setValue", wareID);
}

function easyUIComboboxOnChange(newValue, oldValue) {
    if (newValue.length == 0)
        return;
  
    var arr = newValue.toString().split(",");
    var data = [];
    for (var i = 0; i < newValue.length; i++) {
        if (newValue[i] != "") {
            data.push(newValue[i]);
        }
    }
    if (data.length == 0) {
        data.push("");
    }
    $(this).combobox("setValues", data);
}

//combotree扩展
/**  * combobox和combotree模糊查询  */ 
(function(){     //combobox可编辑，自定义模糊查询     
    $.fn.combobox.defaults.editable = true;     
    $.fn.combobox.defaults.filter = function(q, row){         
        var opts = $(this).combobox('options');         
        return row[opts.textField].indexOf(q) >= 0;     
    };     
    //combotree可编辑，自定义模糊查询     
    $.fn.combotree.defaults.editable = true;
    $.extend($.fn.combotree.defaults.keyHandler,
        {
            up: function () { console.log('up'); },
            down: function () { console.log('down'); },
            enter: function () { console.log('enter'); },
            query: function (q) {
                var t = $(this).combotree('tree');
                var nodes = t.tree('getChildren');
                for (var i = 0; i < nodes.length; i++) {
                    var node = nodes[i];
                    if (node.text.indexOf(q) >= 0) {
                        $(node.target).show();
                    } else { $(node.target).hide(); }
                }
                var opts = $(this).combotree('options');
                if (!opts.hasSetEvents) {
                    opts.hasSetEvents = true;
                    var onShowPanel = opts.onShowPanel;
                    opts.onShowPanel = function () {
                        var nodes = t.tree('getChildren');
                        for (var i = 0; i < nodes.length; i++) {
                            $(nodes[i].target).show();
                        }
                        onShowPanel.call(this);
                    };
                    $(this).combo('options').onShowPanel = opts.onShowPanel;
                }
            }
        });


    //修改easyUI combobox 可填写值bug
    $.extend($.fn.combobox.methods, {
        //替换easyui getValue
        getFilterValue: function (e) {
            var data = e.combobox("getData");
            var selValue = e.combobox("getValue");
            if (!selValue)
                return selValue;
            if (data && data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    if (data[i][e.combobox("options").valueField] == selValue) {
                        return selValue;
                    }
                }
            }
            e.combobox("setValues", "");
            return "";
        },
        //替换easyui getValues
        getFilterValues: function (e) {
            var data = e.combobox("getData");
            var selValues = e.combobox("getValues");
            if (!selValues || selValues.length == 0)
                return selValues;

            var newData = [];
            for (var j = 0; j < selValues.length; j++) {
                if (data && data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        if (data[i][e.combobox("options").valueField] == selValues[j]) {
                            newData.push(selValues[j]);
                        }
                    }
                }
            }
            e.combobox("setValues", newData);
            return newData;
        },
        getFilterJoinStringValues: function (e) {
            return e.combobox("getFilterValues").join(",");
        }
    }, true);
})(jQuery);